﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;

namespace weikebuluo.wxgzpt.wechat.orderfood
{
    public partial class myfood : System.Web.UI.Page
    {
        a_orderfood_dal orderfooddal = new a_orderfood_dal();
        a_orderfood_model orderfoodmodel = new a_orderfood_model();

        a_order_dal orderdal = new a_order_dal();
        a_order_model ordermodel = new a_order_model();

        JsonOperate JsonOperate = new JsonOperate();
        protected int count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["wxid"]))
            {
                count = orderfooddal.GetRecordCount("wxid='" + Request.QueryString["wxid"] + "' and orderfood_stutas=0 ");
            }
            switch (Request.QueryString["action"]) { 
                case"list":
                    list();
                    break;
            }
            switch (Request.Form["action"]) {
                case "add":
                    add();
                    break;
            }
        }
        private void list() {
            if (!string.IsNullOrEmpty(Request.QueryString["wxid"]))
            {
                DataTable dtorderfood = orderfooddal.GetList1("wxid='" + Request.QueryString["wxid"] + "' and orderfood_stutas=0").Tables[0];
                Response.Write(JsonOperate.TableToBasicJson(dtorderfood));
            }
            Response.End();
        }
        private void add() {
            if (!string.IsNullOrEmpty(Request.Form["orderfood_id"]))
            {
                ordermodel.order_allprice = decimal.Parse(Request.Form["all"].Trim().ToString());
                ordermodel.order_no = GetVIP_Code();
                ordermodel.order_name = Request.Form["guest_name"].Trim().ToString();
                ordermodel.order_address = Request.Form["remark"].Trim().ToString();
                ordermodel.order_phone = Request.Form["tel"].Trim().ToString();
                ordermodel.store_id = int.Parse(Request.Form["sid"]);
                ordermodel.wxid = Request.Form["wxid"].Trim().ToString();
                ordermodel.order_addtime = DateTime.Now;
                int order_id = orderdal.Add(ordermodel);

                string orderfood_id = Request.Form["orderfood_id"].Trim().ToString();
                string numBox = Request.Form["numBox"].Trim().ToString();
                string[] orderfood_idlist = orderfood_id.Split(',');
                string[] numBoxlist = numBox.Split(',');
                for (int i = 0; i < orderfood_idlist.Length; i++)
                {
                    orderfoodmodel = orderfooddal.GetModel(int.Parse(orderfood_idlist[i]));
                    orderfoodmodel.order_id = order_id;
                    orderfoodmodel.orderfood_stutas = 1;
                    orderfoodmodel.orderfood_num = int.Parse(numBoxlist[i]);
                    orderfooddal.Update(orderfoodmodel);
                }

                Response.Write("<script Language=\"Javascript\">top.location=\"myorder.aspx?sid=" + Request.Form["sid"] + "&wxid=" + Request.Form["wxid"] + "\";");
                Response.Write("</script>");
                Response.End();
            }
            else {
                Response.Write("<script Language=\"Javascript\">alert(\"请至少选择一种商品！\");");
                Response.Write("</script>");
                Response.End();
            }

        }

        #region 生成卡号
        /// <summary>
        /// 生成卡号
        /// </summary>
        /// <returns></returns>
        private string GetVIP_Code()
        {
            Random ran = new Random();

            string VIP_Code = "";

            for (int j = 0; j < 11; j++)
            {
                VIP_Code += ran.Next(0, 9);
            }
            for (int i = 0; 1 == 1; i++)
            {
                int reInt = orderdal.GetRecordCount("order_no='" + VIP_Code + "'");

                if (reInt < 1)
                {
                    break;
                }
                else
                {
                    VIP_Code = "";

                    for (int j = 0; j < 11; j++)
                    {
                        VIP_Code += ran.Next(0, 9);
                    }
                }
            }
            return VIP_Code;
        }
        #endregion
    }
}